<template>
  <el-card>
    <template #header>
      <el-form inline>
        <el-form-item>
          <el-input placeholder="请输入标签名" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button @click="refresh" icon="Refresh">刷新</el-button>
          <el-button @click="getList" type="primary" icon="Search">搜索</el-button>
          <el-button @click="addShow = true" type="success" icon="plus">添加</el-button>
        </el-form-item>
      </el-form>
    </template>

    <el-table :data="list" border>
      <el-table-column type="index" label="#" width="80" align="center" />
      <el-table-column label="标签名">
        <template #default="{row}">
          <el-tag v-if="!row.isEdit">{{row.name}}</el-tag>
          <el-input placeholder="请输入标签名" v-model="row.name" v-else clearable/>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template #default="{row}">
          <el-button type="danger" text  @click="deleteHandel(row.id)">删除</el-button>
          <el-button type="primary" text v-if="!row.isEdit" @click="row.isEdit = true">修改</el-button>
          <el-button type="success" text v-else @click="save(row, false)">保存</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 8px; display: flex; justify-content: flex-end">
      <el-pagination background layout="prev, pager, next, total" :total="total" @current-change="pageChange"/>
    </div>

    <el-dialog v-model="addShow" destroy-on-close width="20%">
      <el-input placeholder="请输入标签名" v-model="name" style="margin: 20px 0"/>
      <template #footer>
        <el-button @click="addShow = false">取消</el-button>
        <el-button @click="save({name}, true)">提交</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>
<script setup>
import {updateAndAddTagApi, tagPageApi, deleteTagApi} from '@/api/tag.js'
import {ref, reactive} from 'vue'
import {ElMessage, ElMessageBox} from "element-plus";
import useMessage from "@/utils/useMessage.js";

const addShow = ref(false);
const name = ref('')

const query = reactive({
  page: 1,
  pageSize: 10,
  name: '',
})

const list = ref([])
const total = ref(0)
const getList = async () => {
  const res = await tagPageApi(query)
  list.value = res.data.map(item => {
    return {
      ...item,
      isEdit: false
    }
  })
  total.value = res.total
}
getList()

const refresh = ()=> {
  query.page = 1
  query.name = ''
  list.value = []
  getList()
}

const save = async (row, isReload) => {
  if(!row.name){
    ElMessage.error('请输入标签名!')
    return
  }
  const res = await updateAndAddTagApi(row)
  useMessage(res)
  addShow.value = false
  if(isReload){
    refresh()
  }else{
    await getList()
  }
}

const deleteHandel = async (id) => {
  await ElMessageBox.confirm('确定删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  const res = await deleteTagApi(id)
  useMessage(res)
  await getList()
}


const pageChange = (page) => {
  query.page = page
  getList()
}


</script>
<style scoped>

</style>